package com.nand.module.function.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.nand.module.function.R;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author wangjt
 * @date 2018/12/10
 */
public class PublishImgAdapter extends RecyclerView.Adapter<PublishImgAdapter.ImgHolder> {

    Context mContext;
    private ArrayList<String> mData = new ArrayList<>();
    private boolean showAdd = true;
    private ImgItemClick imgItemClickListener;

    public PublishImgAdapter(Context context) {
        mContext = context;
    }

    public ArrayList<String> getData() {
        return mData;
    }

    public void setData(ArrayList<String> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        setShowAdd(mData.size() != 3);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ImgHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_image, null);
        return new ImgHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ImgHolder imgHolder, final int position) {
        // 最后一个数字
        if (position == mData.size()) {
            if (!showAdd) {
                imgHolder.rootLayout.setVisibility(View.GONE);
                return;
            }
            imgHolder.img.setImageResource(R.mipmap.add);
            imgHolder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (imgItemClickListener != null) {
                        imgItemClickListener.lastItemClick();
                    }
                }
            });
            imgHolder.rootLayout.setVisibility(View.VISIBLE);
            imgHolder.del.setVisibility(View.GONE);
        } else {
            imgHolder.rootLayout.setVisibility(View.VISIBLE);
            imgHolder.del.setVisibility(View.VISIBLE);
            final String url = mData.get(position);
            Glide.with(mContext).load(url).into(imgHolder.img);
            imgHolder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (imgItemClickListener != null) {
                        imgItemClickListener.itemClick(url, position);
                    }
                }
            });

            imgHolder.del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (imgItemClickListener != null) {
                        imgItemClickListener.delImg(url,position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData.size() + 1;
    }

    class ImgHolder extends RecyclerView.ViewHolder {

        ImageView img;
        ImageView del;
        View rootLayout;

        public ImgHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img_item_iv);
            del = itemView.findViewById(R.id.img_item_del_iv);
            rootLayout = itemView.findViewById(R.id.img_item_cl);
        }
    }

    public void setShowAdd(boolean isShow) {
        showAdd = isShow;
    }

    public void setImgItemClickListener(ImgItemClick itemClickListener) {
        imgItemClickListener = itemClickListener;
    }

    public interface ImgItemClick {

        /**
         * 图片点击
         *
         * @param url      url
         * @param position p
         */
        void itemClick(String url, int position);

        /**
         * 添加图片
         */
        void lastItemClick();


        /**
         * 删除图片
         *
         * @param url url
         */
        void delImg(String url,int position);
    }
}
